package com.hmsg.health.controller.admin;


import com.hmsg.health.bean.Dto.UserDto;
import com.hmsg.health.bean.domain.TaskDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.enums.StatusCode;
import com.hmsg.health.config.Constants;
import com.hmsg.health.dao.UserDao;
import com.hmsg.health.param.ResponseResult;
import com.hmsg.health.security.IgnoreSecurity;
import com.hmsg.health.security.OpenApi;
import com.hmsg.health.service.UserService;
import com.hmsg.health.service.aes.SecurityData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Api(value = "运营系统用户相关", tags = { "运营系统用户相关" })
@Slf4j
@Controller
@RequestMapping("/user")
public class AdminUserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;


    @GetMapping("/list")
    @ApiOperation(value="用户列表", notes="列表" ,httpMethod="GET")
    @ResponseBody
    @IgnoreSecurity
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = false, example = "15600205006", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "unique", value = "邀请码", required = false, example = "1", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "time", value = "时间", required = false, example = "2020-09-09", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "firstResult", value = "起始页", required = true, example = "0", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "maxResult", value = "页容量", required = true, example = "10", dataType = "int", paramType = "query")

    })
    public ResponseResult<UserDto> getUserlist(@RequestParam(value = "phone", required = false) String phone,
                                               @RequestParam(value = "unique", required = false) String unique,
                                               @RequestParam(value = "time", required = false) String time,
                                               @RequestParam(value = "pageNo") Integer pageNo,
                                               @RequestParam(value = "pageSize") Integer pageSize) {
        Integer firstResult = (pageNo-1)*pageSize;
        Integer maxResult =pageSize;

        int totalpages;//总页数
        int total=userDao.findCount(phone,unique,time);
        totalpages=   total/pageSize;
        if(total% pageSize !=0){
            totalpages++;
        }
        List<UserDo> all = userService.findAll(phone,unique,time,firstResult, maxResult);
        UserDto userDto=new UserDto();
        userDto.setTotalpages(totalpages);
        userDto.setUserDoList(all);
        return new ResponseResult<UserDto>(StatusCode.SYSTEM_SUCCESS, userDto);
    }
}
